# -*- coding: utf-8 -*-
'''
@Software: PyCharm
@File    : comments_emotion.py
@Author  : Bryan SHEN
@E-mail  : m18801919240_3@163.com
@Site    : Shanghai, China
@Time    : 2021-11-01
@Description: 
'''

from LiveBarrageSentiment.func.barrage_text_match import BarrageTextMatch
import json

import pandas as pd

textmatch = BarrageTextMatch()

# red_data1 = pd.read_excel("red_comments.xlsx", sheet_name="part1")
# red_data2 = pd.read_excel("red_comments.xlsx", sheet_name="part2")
#
# items1 = red_data1.to_dict(orient='records')
# items2 = red_data2.to_dict(orient='records')

red_data = pd.read_excel("red_petfood_comments.xlsx")
items = red_data.to_dict(orient='records')

print("-----items total : {}-----".format(len(items)))


for i, item in enumerate(items):

    if i % 50000 == 0: print(i)
    res = json.loads(textmatch.run([str(item["comments"])]))
    feedback = res["feedback"]
    item["emotion"] = feedback[0]["emotion"]
    item["subject"] = feedback[0]["subject"]
    item["point"] = feedback[0]["point"]


# print("-----items2 total : {}-----".format(len(items2)))

# for j, item in enumerate(items2):
#
#     if j % 50000 == 0: print(j)
#     res = json.loads(textmatch.run([str(item["comments"])]))
#     feedback = res["feedback"]
#     item["emotion"] = feedback[0]["emotion"]
#     item["subject"] = feedback[0]["subject"]
#     item["point"] = feedback[0]["point"]

columns = ["match_keyword", "post_id", "comments", "emotion", "subject", "point"]

# new_data1 = pd.DataFrame(items1)
# new_data2 = pd.DataFrame(items2)
new_data = pd.DataFrame(items)

output_path = "red_comments_emotion_20211117.xlsx"
with pd.ExcelWriter(output_path, engine='xlsxwriter', options={'strings_to_urls': False}) as writer:
    # new_data1.to_excel(writer, sheet_name='part1', index=False, columns=columns)
    # new_data2.to_excel(writer, sheet_name='part2', index=False, columns=columns)
    new_data.to_excel(writer, index=False, columns=columns)